//
//  ZYLMultiselectView.m
//  Multiselect
//
//  Created by zhuyuelong on 16/7/18.
//  Copyright © 2016年 zhuyuelong. All rights reserved.
//

#import "ZYLMultiselectView.h"
#import "ZYBigCourseTowModel.h"
#define kCell @"cell"
@implementation ZYLMultiselectView

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = ColorRGB(240, 240, 240);

        self.layer.cornerRadius = 5.f;
        self.layer.masksToBounds = YES;
        
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(8, 0, self.frame.size.width - 16, self.frame.size.height - 50) style:UITableViewStylePlain];
        
        self.tableView.delegate = self;
        
        self.tableView.tableHeaderView = [UIView new];
        self.tableView.tableFooterView = [UIView new];
        
        self.tableView.dataSource = self;
        
        self.tableView.showsVerticalScrollIndicator = NO;
        
        self.tableView.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.tableView];
        
         [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:kCell];
        
    }
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, self.tableView.frame.size.height + 10, self.frame.size.width/3, self.frame.size.height - self.tableView.frame.size.height - 15)];
    
    [self addSubview:confirmBtn];
    
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    
    confirmBtn.layer.cornerRadius = 5;
    
    confirmBtn.layer.borderColor = [UIColor blackColor].CGColor;
    
    confirmBtn.layer.borderWidth = 1;
    
    [confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - self.frame.size.width/3 - 20, self.tableView.frame.size.height + 10, self.frame.size.width/3, confirmBtn.frame.size.height)];
    
    [self addSubview:cancelBtn];
    
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
     [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    
    cancelBtn.layer.cornerRadius = 5;
    
    cancelBtn.layer.borderColor = [UIColor blackColor].CGColor;
    
    cancelBtn.layer.borderWidth = 1;
    
    [cancelBtn addTarget:self action:@selector(canceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    

    
    return self;

}

- (NSMutableArray *)selectArray {
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSMutableArray *)dataSourceTow {
    
    if (_dataSourceTow == nil) {
        self.dataSourceTow = [NSMutableArray array];
    }
    return _dataSourceTow;
    
    
}




-(void)confirmBtnClick{
    if (self.selectBlock) {
        self.selectBlock(self.selectArray,self.dataSourceTow);
    }
    
    
    [self disMiss];
    
}

-(void)canceBtnClick{
    
    [self disMiss];

}




#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    self.modelTow = self.dataSource[indexPath.row];
    if ([self.selectArray containsObject:self.modelTow.ID]) {
        cell.btn.selected = YES;
    }else{
        cell.btn.selected = NO;
    }
    
    cell.label.text = self.modelTow.categoryName;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.modelTow = [self.dataSource objectAtIndex:indexPath.row];
    
    BOOL sss = [self.selectArray containsObject:self.modelTow.ID];
    if(sss) {
        
        [self.selectArray removeObject:self.modelTow.ID];
        [self.dataSourceTow removeObject:self.modelTow.categoryName];
        
    }else {
        [self.selectArray addObject:self.modelTow.ID];
        [self.dataSourceTow addObject:self.modelTow.categoryName];
        
    }
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
    
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
    
    header.backgroundColor = ColorRGB(240, 240, 240);
    
    UILabel *lbTitle = [[UILabel alloc] initWithFrame:header.bounds];
    
    [header addSubview:lbTitle];
    
    if (self.heraderStr != nil) {

        lbTitle.text = self.heraderStr;
        
    }else{
    
        lbTitle.text = @"课程小类目";
        
    }
    
    lbTitle.textAlignment = NSTextAlignmentCenter;
    
    
    return header;
    
}

-(void)disMiss{
    
    [self removeFromSuperview];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
